#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using PHSRAG.Insight21.InsightUtilities;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Admin
{
	/// <summary>The Users object contains a list of Person ids in 
	/// it's DataContainer.</summary>
	public class Users : BO.BusinessObject
	{
		#region Constants
		/// <summary>Key used for caching users.</summary>
		private static string UsersKey = GenerateKey.MakeKey("Users_F4658030-E91E-4687-AF43-225978F8BDF9",GenerateKey.Admin);
		#endregion

		#region Constructor
		/// <summary>Allocates memory for the DataContainer.</summary>
		/// <param name="cachePolicy">The cached policy this object will use for cacheing.</param>
		public Users(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet( );
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return UsersKey; } }
		#endregion

		#region BusinessObject Methods
		/// <summary>Caches thhis object based on it's caching policy.</summary>
		public override void Cache()
		{
			CachePolicy.Cache(UsersKey, this);
		}

		/// <summary>UnCaches this object based on it's caching policy.</summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(UsersKey);
		}

		/// <summary>
		/// Resets the DataContainer, calls GetUsers stored procedure to hydrate the 
		/// DataContainer with the resultset, sorts the data. Strips down the dataset
		/// leaving only the ids if requested.
		/// </summary>
		/// <param name="connectionString">A valid database connection string.</param>
		/// <param name="args">SortInfo, ResultSetType, Username, Last Name, First Name</param>
		public override void Load(string connectionString, params object[] args)
		{

			try 
			{
				if (args.Length != 6)
					throw new Exception("SortInfo, ResultSetType, Username, Last Name, First Name, Include UnRegistered users must be specified.");

				DataContainer.Reset();

				DataSet dataSet = DataAccess.GetDataSet(connectionString, "GetUsers",
					DataAccess.BuildSqlParameterArray("@username", SqlDbType.VarChar, args[2],
					"@lastName", SqlDbType.VarChar, args[3],
					"@firstName", SqlDbType.VarChar, args[4],
					"@includeUnRegisteredUsers", SqlDbType.Bit, args[5]));

				DataView dataView = new DataView(dataSet.Tables[0]);
				dataView.Sort = (string)args[0];
				dataView.RowStateFilter = DataViewRowState.CurrentRows;

				if ((ResultSetType)args[1] == ResultSetType.IDsOnly)
					for (int i = dataView.Table.Columns.Count - 1; i > 0; --i)
						if (dataView.Table.Columns[i].ColumnName != "PersonId")
							dataView.Table.Columns.RemoveAt(i);

				DataTable dataTable = dataView.Table.Clone();
				foreach (DataRowView dataRowView in dataView)
					dataTable.ImportRow(dataRowView.Row);

				DataContainer.Tables.Add(dataTable);
			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("Users Load", e, true, Utility.ExceptionReport.ReportType.Error);
			}

		}

		#endregion
	}
}

// --- EOF ---
